package com.ssh.manager.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Activity;
import com.ssh.entity.ActivityImage;
import com.ssh.entity.ActivitySuperMarket;
import com.ssh.entity.Company;
import com.ssh.entity.Position;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.SuperMarketProduct;
import com.ssh.manager.ActivityImageManager;
import com.ssh.manager.ActivityManager;

@Service
public class ActivityImageManagerImpl implements ActivityImageManager {
	@Resource
	private IDao daoImpl;

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from ActivityImage";
		String findSql = "from ActivityImage";
		if (conditionMap.get("activity")!=null && !conditionMap.get("activity").equals("")) {
			sql += " and activitySuperMarket.activity.content like '%"+conditionMap.get("activity")+"%'";
			findSql += " and activitySuperMarket.activity.content like '%"+conditionMap.get("activity")+"%'";
		}
		if (conditionMap.get("market")!=null && !conditionMap.get("market").equals("")) {
			sql += " and activitySuperMarket.superMarket.id="+conditionMap.get("market");
			findSql += " and activitySuperMarket.superMarket.id="+conditionMap.get("market");
		}
		sql = sql.replaceFirst("and", "where");
		findSql = findSql.replaceFirst("and", "where");
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<ActivityImage> activityImages = (List<ActivityImage>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(activityImages);
	}

	@Override
	public boolean delete(Long id) {
		try {
			daoImpl.deleteDomain(ActivityImage.class, id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public ActivitySuperMarket saveActivitySuperMarket(ActivitySuperMarket activitySuperMarket_) {
		// TODO Auto-generated method stub
		return daoImpl.saveOrUpdateDomain(activitySuperMarket_);
	}

	@Override
	public ActivityImage saveActivityImage(ActivityImage activityImage) {
		// TODO Auto-generated method stub
		return daoImpl.saveOrUpdateDomain(activityImage);
	}

	@Override
	public ActivitySuperMarket loadActivitySuperMarket(long id) {
		// TODO Auto-generated method stub
		return daoImpl.getDomain(ActivitySuperMarket.class, id);
	}

	
	public List<ActivityImage> findActivityImages(){
		Calendar calender=Calendar.getInstance();
		calender.setTimeInMillis(System.currentTimeMillis());
		calender.set(Calendar.HOUR_OF_DAY, 0);
		calender.set(Calendar.MINUTE, 0);
		calender.set(Calendar.SECOND, 0);
		return (List<ActivityImage>)daoImpl.findListBySql("from ActivityImage where activitySuperMarket.activity.endDate>=?", new Object[]{new Timestamp(calender.getTimeInMillis())}, false, true, false,0,-1);
	}
	

	
}
